<template>
  <div>
    <van-nav-bar title="登录" class="navbar"></van-nav-bar>
    <van-form @submit="onSubmit" ref="form">
      <van-field
        v-model="mobile"
        name="mobile"
        placeholder="请输入用户名"
        :rules="mobileRules"
      >
        <template #label>
          <span class="iconfont icon-shouji"></span>
        </template>
      </van-field>
      <van-field
        v-model="code"
        name="code"
        placeholder="请输入验证码"
        :rules="codeRules"
      >
        <template #label>
          <span class="iconfont icon-yanzhengma"></span>
        </template>
        <template #button>
          <van-button
            class="code-btn"
            type="default"
            size="small"
            round
            native-type="button"
            @click="getCode"
            v-if="isShow"
            >获取验证码</van-button
          >
          <van-count-down
            :time="5 * 1000"
            format="ss s"
            v-else
            @finish="isShow = true"
          />
        </template>
      </van-field>
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { mapMutations } from 'vuex'
import { loginAPI, getCodeAPI } from '@/api'
export default {
  data() {
    return {
      mobileRules: [
        {
          required: true,
          message: '请输入手机号',
          trigger: 'onChange'
        },
        {
          pattern:
            /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
          message: '手机号格式不正确',
          trigger: 'onChange'
        }
      ],
      codeRules: [
        {
          required: true,
          message: '请输入验证码'
        },
        {
          pattern: /^[0-9]{6}$/,
          message: '请输入6位验证码'
        }
      ],
      mobile: '',
      code: '',
      isShow: true
    }
  },
  methods: {
    ...mapMutations(['SET_TOKEN']),
    async onSubmit() {
      // console.log(res);
      this.$toast.loading({
        message: '加载中。。。',
        forbidClick: true
      })
      try {
        const { data } = await loginAPI(this.mobile, this.code)
        this.SET_TOKEN(data.data)
        this.$toast.success('登录成功')
        this.$router.push('/profile')
      } catch (error) {
        if (error.response?.status === 400) {
          this.$toast.fail(error.response.data.message)
        } else {
          throw error
        }
      }
    },
    async getCode() {
      await this.$refs.form.validate('mobile')
      this.isShow = false
      this.$toast.loading({
        message: '发送中。。。',
        forbidClick: true
      })
      try {
        const res = await getCodeAPI(this.mobile)
        this.$toast.success('发送成功')
        console.log(res)
      } catch (error) {
        if (error.response?.status === 429) {
          this.$toast.fail(error.response.data.message)
        }
      }
    }
  }
}
</script>

<style scoped lang="less">
.navbar {
  background-color: #3296fa;
  //样式穿透 vue-cli提供的
  :deep(.van-nav-bar__title) {
    color: #fff;
  }
}

:deep(.van-form) {
  .iconfont {
    font-size: 40px;
  }
  .van-field__label {
    flex: 1;
  }
  .van-field__value {
    flex: 20;
  }
}

.code-btn {
  background-color: #ededed;
  font-size: 14px;
  color: #666;
}
</style>